package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.store.add.AppStoreFreightTemplateAddReq;
import com.black.app.domain.req.store.edit.AppStoreFreightTemplateEditReq;
import com.black.app.domain.req.store.query.AppStoreFreightTemplateQueryReq;
import com.black.app.domain.resp.store.AppStoreFreightTemplateResp;
import com.black.app.domain.store.AppStoreFreightTemplate;
import com.black.app.service.store.AppStoreFreightTemplateService;
import com.black.app.mapper.store.AppStoreFreightTemplateMapper;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author zhous
* @description 针对表【app_store_freight_template】的数据库操作Service实现
* @createDate 2024-11-01 16:35:34
*/
@Service
public class AppStoreFreightTemplateServiceImpl extends ServiceImpl<AppStoreFreightTemplateMapper, AppStoreFreightTemplate>
    implements AppStoreFreightTemplateService{

    /**
     * 分页查询
     * @param req
     * @return
     */
    @Override
    public PageInfo<AppStoreFreightTemplateResp> paginationQuery(AppStoreFreightTemplateQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(()->{
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 新增
     * @param req
     * @return
     */
    @Override
    public boolean add(AppStoreFreightTemplateAddReq req) {
        AppStoreFreightTemplate newEntity = new AppStoreFreightTemplate();
        // 对象拷贝
        BeanUtils.copyProperties(req, newEntity);
        return this.save(newEntity);
    }

    /**
     * 根据主键查询
     * @param templateId
     * @return
     */
    @Override
    public AppStoreFreightTemplateResp findByTemplateId(Integer templateId) {
        if (Objects.isNull(templateId)){
            throw new PrimaryException();
        }
        // 根据主键查询
        AppStoreFreightTemplate appStoreFreightTemplate = this.getById(templateId);
        if (Objects.isNull(appStoreFreightTemplate)){
            throw new RecordNotFoundException();
        }
        AppStoreFreightTemplateResp resp = new AppStoreFreightTemplateResp();
        BeanUtils.copyProperties(appStoreFreightTemplate, resp);
        return resp;
    }

    /**
     * 修改
     * @param req
     * @return
     */
    @Override
    public boolean edit(AppStoreFreightTemplateEditReq req) {
        // 根据主键查询
        AppStoreFreightTemplate appStoreFreightTemplate = this.getById(req.getTemplateId());
        // 判断 查找不到数据
        if (Objects.isNull(appStoreFreightTemplate)){
            throw new RecordNotFoundException();
        }
        // 对象拷贝
        BeanUtils.copyProperties(req, appStoreFreightTemplate);
        // 更新
        return this.updateById(appStoreFreightTemplate);

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteById(Integer[] ids) {
        if (Objects.isNull(ids) || ids.length == 0){
            throw new PrimaryException();
        }
        for (Integer templateId : ids) {
            AppStoreFreightTemplate appStoreFreightTemplate = this.getById(templateId);
            if (Objects.nonNull(appStoreFreightTemplate)){
                this.removeById(templateId);
            }
        }
        return true;
    }


}




